package com.hundsun.yr.universal.p2p.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.Utils.HeaderMap;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.QRCodeUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.utils.Log;

import java.lang.ref.WeakReference;
import java.util.HashMap;


/**
 * Created by xx on 2017/7/4.
 * 邀请好友
 */
public class InviteFrientsActivity extends BaseActivity implements View.OnClickListener{
    /**
     * 邀请好友二维码
     */
    private ImageView iv_qcode;

    /**
     * 微信推荐
     */
    private LinearLayout linear_wechat;

    /**
     * 微信朋友圈
     */
    private LinearLayout linear_wxcircle;

    /**
     * 返回按钮
     */
    private ImageView invite_friends_back;

    /**
     * 邀请好友列表页面
     */
    private ImageView invite_friends_more;

    /**
     * 邀请人的号码
     */
    private TextView tv_invited_phone_num;
    private String zwgt_data;
    private RelativeLayout rl_invite_frient_title;

    private ShareAction mShareAction;

    private UMShareListener mShareListener;

    private TextView tv_frient_text;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_mine_share;
    }

    @Override
    protected void initViewsAndEvents() {
        setToolbarTitle(getString(R.string.mine_recommend));

        iv_qcode = findView(R.id.iv_qcode);
        linear_wechat = findView(R.id.linear_wechat);
        linear_wxcircle = findView(R.id.linear_wxcircle);
        invite_friends_back = findView(R.id.invite_friends_back);
        invite_friends_more = findView(R.id.invite_friends_more);
        invite_friends_more.setOnClickListener(this);
        tv_invited_phone_num = findView(R.id.tv_invited_phone_num);
        tv_frient_text = findView(R.id.tv_frient_text);
        if (sharedPreferences.getShareProf(UserInfo.FRIENT_TEXT) != null && !sharedPreferences.getShareProf(UserInfo.FRIENT_TEXT).isEmpty()){
            tv_frient_text.setText(sharedPreferences.getShareProf(UserInfo.FRIENT_TEXT));
        }

        tv_invited_phone_num.setText(getString(R.string.user_RQ_code, CommTool.encodPhone(sharedPreferences.getShareProf(UserInfo.MOBILE))));
        String url = getString(R.string.root_h5_url_test) + "/h5/actives/register.html?activity=1&&channel=RKxWS7f7&code=" + sharedPreferences.getShareProf(UserInfo.MOBILE);
        Resources res = getResources();
        Bitmap bmp = BitmapFactory.decodeResource(res,R.drawable.icon);
        Bitmap bitmap1 = QRCodeUtil.createQrCodeLogo(url, CommTool.dip2px(mContext,150),bmp);
        iv_qcode.setImageBitmap(bitmap1);

        linear_wechat.setOnClickListener(this);
        linear_wxcircle.setOnClickListener(this);
        invite_friends_back.setOnClickListener(this);

        mShareListener = new CustomShareListener(InviteFrientsActivity.this);
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.rl_invite_frient_title).init();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void onClick(View view) {
        if (isFastDoubleClick()){
            return;
        }

        switch (view.getId()){
            case R.id.linear_wechat://分享微信
                MobclickAgent.onEvent(this,"invite");
//                mShareAction.open();
                UMWeb web = new UMWeb(getString(R.string.root_h5_url_test) + "/h5/index/index.html#!/register/invite/" + sharedPreferences.getShareProf(UserInfo.MOBILE));
                web.setTitle("【中网国投】");
                web.setDescription("立即注册即可领取5元现金券，首次出借后还送20元，你还等什么？");
                web.setThumb(new UMImage(InviteFrientsActivity.this, R.drawable.icon));

                new ShareAction(this)
                        .withMedia(web)
                        .setPlatform(SHARE_MEDIA.WEIXIN)
                        .setCallback(mShareListener).share();

                //试一下分享新浪微博
//                new ShareAction(this)
//                        .withTitle("分享中网国投")
//                        .withText("立即注册即可领取5元现金券，首次投资后还送20元，你还等什么？")
//                        .withTargetUrl(getString(R.string.root_url_test) + "/h5/index/index.html#!/register/invite/" + sharedPreferences.getShareProf(UserInfo.MOBILE))
//                        .withMedia(new UMImage(mContext, R.drawable.icon))
//                        .setPlatform(SHARE_MEDIA.SINA)
//                        .setCallback(umShareListener).share();
                break;

            case R.id.linear_wxcircle://分享朋友圈
//                mShareAction.open();
                MobclickAgent.onEvent(this,"invite");
                UMWeb web2 = new UMWeb(getString(R.string.root_h5_url_test) + "/h5/index/index.html#!/register/invite/" + sharedPreferences.getShareProf(UserInfo.MOBILE));
                web2.setTitle("【中网国投】");
                web2.setDescription("立即注册即可领取5元现金券，首次出借后还送20元，你还等什么？");
                web2.setThumb(new UMImage(InviteFrientsActivity.this, R.drawable.icon));

                new ShareAction(this)
                        .withMedia(web2)
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                        .setCallback(mShareListener).share();

                break;
            case R.id.invite_friends_back:
                finish();
                break;
            case R.id.invite_friends_more:
//                showToast("邀请好友列表展示");
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                HashMap<String, String> hearder = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                        TaskConstants.TASK_INVITEDLIST, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
                HeaderMap headerMap = new HeaderMap();
                headerMap.setMap(hearder);
                Bundle bundle = new Bundle();
                bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, getString(R.string.invite_persons));
                bundle.putString(BaseWebActivity.BUNDLE_KEY_URL, getString(R.string.root_h5_url_test) + getString(R.string.h5_user) + TaskConstants.TASK_INVITEDLIST);
                bundle.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap);
                bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.HTTP.getIntValue());
                bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
                readyGo(WebActivity.class, bundle);
                break;
        }
    }

    private static class CustomShareListener implements UMShareListener {

        private WeakReference<MainActivity> mActivity;

        private CustomShareListener(BaseActivity activity) {
            mActivity = new WeakReference(activity);
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {

            if (platform.name().equals("WEIXIN_FAVORITE")) {
                Toast.makeText(mActivity.get(), platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
            } else {
                if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                        && platform != SHARE_MEDIA.EMAIL
                        && platform != SHARE_MEDIA.FLICKR
                        && platform != SHARE_MEDIA.FOURSQUARE
                        && platform != SHARE_MEDIA.TUMBLR
                        && platform != SHARE_MEDIA.POCKET
                        && platform != SHARE_MEDIA.PINTEREST

                        && platform != SHARE_MEDIA.INSTAGRAM
                        && platform != SHARE_MEDIA.GOOGLEPLUS
                        && platform != SHARE_MEDIA.YNOTE
                        && platform != SHARE_MEDIA.EVERNOTE) {
                    Toast.makeText(mActivity.get(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                }

            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                    && platform != SHARE_MEDIA.EMAIL
                    && platform != SHARE_MEDIA.FLICKR
                    && platform != SHARE_MEDIA.FOURSQUARE
                    && platform != SHARE_MEDIA.TUMBLR
                    && platform != SHARE_MEDIA.POCKET
                    && platform != SHARE_MEDIA.PINTEREST

                    && platform != SHARE_MEDIA.INSTAGRAM
                    && platform != SHARE_MEDIA.GOOGLEPLUS
                    && platform != SHARE_MEDIA.YNOTE
                    && platform != SHARE_MEDIA.EVERNOTE) {
                Toast.makeText(mActivity.get(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
                if (t != null) {
                    Log.d("throw", "throw:" + t.getMessage());
                }
            }

        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {

            Toast.makeText(mActivity.get(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    }

//    private UMShareListener umShareListener = new UMShareListener() {
//        @Override
//        public void onResult(SHARE_MEDIA platform) {
//            showToast("分享成功了");
//
//        }
//
//        @Override
//        public void onError(SHARE_MEDIA platform, Throwable t) {
//            showToast("分享失败了");
//            if(t!=null){
//                Log.d("throw","throw:"+t.getMessage());
//            }
//        }
//
//        @Override
//        public void onCancel(SHARE_MEDIA platform) {
////	        Toast.makeText(MineShareActivity.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
//            showToast("分享取消了");
//        }
//    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 防止按钮连续点击
     */
    private static long lastClickTime;
    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        if ( time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("InviteFrientsActivity");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("InviteFrientsActivity");
        MobclickAgent.onPause(this);
    }
}
